package org.credo.emayMMS;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class IOTest {

//	private static final String SMIL_MSG = "<smil xmlns=\"http://www.w3.org/2000/SMIL20/CR/Language\"><head><layout><root-layout width=\"208\" height=\"176\" /><region id=\"Image\" left=\"0\" top=\"0\" width=\"128\" height=\"128\" fit=\"hidden\" /><region id=\"Text\" left=\"0\" top=\"50\" width=\"128\" height=\"128\" fit=\"hidden\" /></layout></head><body><par dur=\"25000ms\"><text src=\"1.txt\" region=\"Text\" /></par></body></smil>";

	private static final int clientNum=2;
	
	public static void main(String[] args) {
	        ExecutorService exec = Executors.newCachedThreadPool();
	        // 10个线程可以同时访问
	        final Semaphore semp = new Semaphore(2);
	        // 模拟20个客户端访问
	        for (int index = 0; index < clientNum; index++) {
	            final int NO = index;
	            Runnable run = new Runnable() {
	                public void run() {
	                    try {
	                        // 获取许可
	                        semp.acquire();
	                        System.out.println("Thread:" + NO);
	                        IOTest.sendSMS(NO, "wsadwefwefef");
	                        //System.out.println("zipName:"+zipName);
	                        // 释放
	                        System.out.println("第：" + NO + " 个");
	                        semp.release();
	                    } catch (Exception e) {
	                        e.printStackTrace();
	                    }
	                }
	            };
	            exec.execute(run);
	        }
	        // 退出线程池
	        exec.shutdown();
	}

	public static String sendSMS(int phoneNumber, String message) {
		String zipName = null;
		try {
			String filePath=System.getProperty("java.io.tmpdir");
			System.out.println(filePath);
			String fileName=UUID.randomUUID().toString();
			System.out.println("fileName:"+fileName);
			String SMIL_MSG = "<smil xmlns=\"http://www.w3.org/2000/SMIL20/CR/Language\"><head><layout><root-layout width=\"208\" height=\"176\" /><region id=\"Image\" left=\"0\" top=\"0\" width=\"128\" height=\"128\" fit=\"hidden\" /><region id=\"Text\" left=\"0\" top=\"50\" width=\"128\" height=\"128\" fit=\"hidden\" /></layout></head><body><par dur=\"25000ms\"><text src=\""+fileName+".txt\" region=\"Text\" /></par></body></smil>";
			String txtPath=filePath+fileName+".txt";
			String smilPath=filePath+fileName+".smil";
			String zipPath=filePath+fileName+".zip";
			
			
			// 第一个文件1.txt,字符流,文件指定编码为GB2312,对应windows的ANSI编码,否则发送出去的彩信为乱码.
			PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(txtPath), "GB2312"));
			pw.print(message);
			pw.close();
			// 第二个文件 test.smil,字符流
			PrintWriter pw2 = new PrintWriter(new FileWriter(smilPath));
			pw2.print(SMIL_MSG);
			pw2.close();
			// 压缩
			File f1 = new File(txtPath);
			File f2 = new File(smilPath);
			File[] srcfile = { f1, f2 };
			// 压缩后的文件
			File zipfile = new File(zipPath);
			zipFiles(srcfile, zipfile);
			zipName=zipfile.getName();
			System.out.println("压缩后文件名:"+zipName);
			// 字节流
			FileInputStream fs = new FileInputStream(zipfile);
			byte[] Content = new byte[fs.available()];
			fs.read(Content, 0, Content.length);
			fs.close();
//			f1.delete();
//			f2.delete();
//			zipfile.delete();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
		return zipName;
	}

	/**
	 * 压缩多个文件.java原生ZIP处理.
	 */
	private static void zipFiles(File[] srcfile, File zipfile) throws IOException {
		byte[] buf = new byte[1024];
		// ZipOutputStream类：完成文件或文件夹的压缩
		ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
		for (File element : srcfile) {
			FileInputStream in = new FileInputStream(element);
			out.putNextEntry(new ZipEntry(element.getName()));
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			out.closeEntry();
			in.close();
		}
		out.close();
		//System.out.println("压缩成功!");
	}
}
